<?php 
	session_start();
	include("include/db_connect.php");
	include("include/header.php");
	include("include/menu.php");
?>


	<!-- Start of Main Content Area -->
	
	

<?php 	
echo "<div id='search'>";



echo "<br><br>";

echo "<h1 class= 'zoek'> Zoeken </h1>";

echo "<br><br><br>";

echo 	"<form name='form' action='search.php' method='get'>";
echo 	"<input type='text' name='search'>";
echo 	"<input type='submit' name='Submit' value='Zoeken'>";
echo 	"</form></center><br><br>"; 

 
		if(!isset($_GET['page']) || $_GET['page'] < 0) {
			$_GET['page'] = 1;
			if(isset($_GET['search'])) {
				header("Location: search.php?page=1&search=".$_GET['search']);
			} else {
				header("Location: search.php?page=1&search=".$_GET['search']);
			}
		}
	

  $var = mysql_escape_string($_GET['search']);
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<br><h1 class='center'>Please enter a search...</h1>";
  echo "</div>";
  include "include/footer.php";
  exit;
  }


	$table= 'PRODUCT';
	$field_sql = 'SHOW COLUMNS FROM '.$table;
    $field_q = mysql_query($field_sql,$con);
	if (mysql_num_rows($field_q) > 0) {
		$i=0;
		  while ($row = mysql_fetch_array($field_q)) 
		   {
		if($i==0)
		{
			$q="select * from $table where ".$row[0]." REGEXP('".$trimmed."')";
			$columns = $row[0]." REGEXP('".$trimmed."')";
		}

		else
		{
			$q.=" or ".$row[0]." REGEXP('".$trimmed."')";
			$columns .= " or ".$row[0]." REGEXP('".$trimmed."')";
		}
		$i++;
		
		}
		$productCountQuery = mysql_query("SELECT COUNT(idPRODUCT) FROM PRODUCT WHERE ".$columns);
		if(!$productCountQuery) {
			die("Query failed, please try again later. ".mysql_error());
		}
}
								$productPerPageCount= 15;
								if($_GET['page'] == 1) {
										$lowerLimit = 0;
								} else {
								$lowerLimit = 0;
								for($i = 2; $i <= $_GET['page']; $i++) {
								$lowerLimit = $lowerLimit + $productPerPageCount;
								}
								}
								$limit = $lowerLimit.",".$productPerPageCount;
								
$field_t = mysql_query($q.'limit '.$limit);
if(!$field_t){
die("Query failed ".mysql_error());
}

$field_t1 = mysql_fetch_assoc($field_t);
		if (mysql_num_rows($field_t) == 0)
		  {
		  echo "<center>";
		  echo "<br><h2 class='center1'>Geen zoekresultaten</h2>";
		  echo "<br><div class='center2'><center>Uw zoekopdracht heeft geen resultaten opgeleverd!</center></div>";
		  echo "</center>";
		  echo "</div>";
		  include "include/footer.php";
		  exit;
		  }
echo "<table id='product_table'>";
		echo "<tr>";
		echo "<th></th>";
		echo "<th class='artikelnr'>Artikelnr.</th>";
		echo "<th class='omschrijving'>Omschrijving</th>";
		echo "<th class='prijs'>Prijs</th>";
		echo "<th class='artikelnr'>Aanbieding?</th>";
		echo "<th class='bestel'></th>";
		echo "</tr>";
		while($row = mysql_fetch_array($field_t)) {
			$omschrijving = substr($row['omschrijving'], 0, 100)."...";
			echo "<tr>";
			echo "<td><a href='productdetail.php?id=".$row['idPRODUCT']."'>Details</a></td>";
			echo "<td>".$row['idPRODUCT']."</td>";
			echo "<td>".$omschrijving."</td>";
			echo "<td>"."&euro; ".str_replace(".", ",", $row['prijs'])."</td>";
			if($row['korting'] != 0) {
				echo "<td>Ja!</td>";
			} else {
				echo "<td>Nee</td>";
			}
			echo "<td><a href='winkelwagen.php?id=".$row['idPRODUCT']."&action=add'>Bestel!</a></td>";
			echo "</tr>";
		}
		echo "</table>";
		echo "<br>";
		echo "<div class='nav'>";
		if($_GET['page'] != 1) {
			$page = $_GET['page'] - 1;
		
			echo "<a href='?page=".$page."&search=".$_GET['search']."'>Vorige</a>\n";
		}
		
		$productCount = mysql_result($productCountQuery, 0);
		if($productCount > $productPerPageCount) {
			//hier wordt uitgerekend hoeveel pagina's er nodig zijn
			//ceil = omhoog afronden
			$pagesCount = ceil($productCount / $productPerPageCount);
			for($i = 1; $i <= $pagesCount; $i++) {
				if($_GET['page'] == $i) {
					$pageNav = "<span class='activePage'>".$i."</span>&nbsp;";
				} else {
					$pageNav = "<a href='?page=".$i."&search=".$_GET['search']."'>".$i."</a>&nbsp;";
				}
				echo $pageNav;
			}
			if($pagesCount != $_GET['page']) {
				$page = $_GET['page'] + 1;
				echo "<a href='?page=".$page."&search=".$_GET['search']."'>Volgende</a>";
			}
		}



echo "</div>";
echo "</div>";
echo "<div class='clearthis'>&nbsp;</div>";

include("include/footer.php");
?>

	